package com.GlobalPaint.app.ui.Home.friend;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.GlobalPaint.app.Adapter.MyFensiAdapter;
import com.GlobalPaint.app.R;
import com.GlobalPaint.app.bean.EventBusben;
import com.GlobalPaint.app.bean.MyFriendBean;
import com.GlobalPaint.app.bean.ReturnStatusResultEntity;
import com.GlobalPaint.app.utils.Constants;
import com.GlobalPaint.app.utils.DataManager;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;


public class FensiFragment extends Fragment {

    @BindView(R.id.xrecycler)
    XRecyclerView xrecycler;
    Unbinder unbinder;
    private MyFensiAdapter adapter;
    private MyFriendBean bean;
    private LinearLayoutManager linearLayoutManager;
    private List<MyFriendBean.DataBean> list;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_fensi, container, false);
        unbinder = ButterKnife.bind(this, view);
        linearLayoutManager = new LinearLayoutManager(getContext());
        xrecycler.setLayoutManager(linearLayoutManager);
        xrecycler.setLoadingMoreEnabled(false);
        getguanzhu();
        initLoad();
        return view;
    }

    public void getfensi(String nick, String jid, String userName){
        String url= Constants.JASON_SERVICE_URL+"appFriends/addAttention?userName="
                +userName+"&jid="+jid+"&nick="+nick;
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), MyFriendBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {

                bean=(MyFriendBean)o;
                if (bean.getStatus() == 1){
                    getguanzhu();
                    EventBusben baseMessage = new EventBusben("haoyou");
                    EventBus.getDefault().post(baseMessage);
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                xrecycler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {

            }
        });

    }
    private void getguanzhu() {

        String url= Constants.JASON_SERVICE_URL+"appFriends/getFans?userName="+ DataManager.userEntity.getUserName();
        DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), MyFriendBean.class, new DataManager.Callback() {
            @Override
            public void onSuccess(Object o) {

                bean=(MyFriendBean)o;
                if (bean.getStatus() == 1){
                    if(bean.getData() != null && bean.getData().size()>0){
                        list=bean.getData();
                        adapter=new MyFensiAdapter(list,FensiFragment.this);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }else {
                        list=new ArrayList<MyFriendBean.DataBean>();
                        adapter=new MyFensiAdapter(list,FensiFragment.this);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                xrecycler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {

                if(bean !=null){
                    if(bean.getData() != null && bean.getData().size()>0) {
                        bean.getData().clear();
                        Toast.makeText(getContext(), bean.getMsg(), Toast.LENGTH_LONG).show();
                        adapter.notifyDataSetChanged();
                        xrecycler.refreshComplete();
                    }
                }

            }
        });

    }
    private void initLoad() {
        xrecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                String url= Constants.JASON_SERVICE_URL+"appFriends/getFans?userName="+ DataManager.userEntity.getUserName();
                DataManager.getInst().getHttpRequestJsonClass(url, DataManager.userEntity.getCookie(), MyFriendBean.class, new DataManager.Callback() {
                    @Override
                    public void onSuccess(Object o) {
                        MyFriendBean talentBean=(MyFriendBean)o;
                        if( bean.getStatus() == 1){
                            if (bean.getData() != null && bean.getData().size()>0){
                                bean.getData().clear();
                                bean.getData().addAll(talentBean.getData());
                                adapter.notifyDataSetChanged();
                                xrecycler.loadMoreComplete();
                            }
                        }
                        else {
                            Toast.makeText(getContext(),bean.getMsg(),Toast.LENGTH_LONG).show();
                        }
                        xrecycler.refreshComplete();
                    }

                    @Override
                    public void onFailure(ReturnStatusResultEntity error) {
                        xrecycler.refreshComplete();
                    }
                });
            }

            @Override
            public void onLoadMore() {

            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
